package com.yiwei.zhh.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

/**
 */
public class UI {
    private static long _lastClickTime;

    /**
     * fast double click
     */
    public static boolean isFastClick() {
        long now = System.currentTimeMillis();
        long diff = now - _lastClickTime;
        if (0 < diff && diff < 500) {
            return true;
        }
        _lastClickTime = now;
        return false;
    }

    public static boolean isFastClick(long limit) {
        long now = System.currentTimeMillis();
        long diff = now - _lastClickTime;
        if (0 < diff && diff < limit) {
            return true;
        }
        _lastClickTime = now;
        return false;
    }

    public void showAtBottom(Dialog dialog) {
        WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
        wmlp.gravity = Gravity.BOTTOM;
        dialog.show();
    }

    public void showAtLocation(Dialog dialog, int x, int y) {
        WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
        wmlp.gravity = Gravity.TOP | Gravity.LEFT;
        wmlp.x = x;
        wmlp.y = y;
        dialog.show();
    }

    public static void showAsDropDown(Dialog dialog, View view) {
        dialog.show();
        WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

        final Rect rect = new Rect();
        view.getWindowVisibleDisplayFrame(rect);

        int[] location = new int[2];
        view.getLocationInWindow(location);
        wmlp.gravity = Gravity.TOP | Gravity.LEFT;
        wmlp.x = location[0];
        wmlp.y = location[1] - rect.top + view.getHeight();
        wmlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(wmlp);
    }

    /**
     * show toast
     */
    public static void showToast(Context context, String content) {
        Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
    }

    public static void showToast(Context context, int id) {
        Toast.makeText(context, context.getResources().getString(id), Toast.LENGTH_SHORT).show();
    }

    public static void showToast(Context context, int id, Object... values) {
        Toast.makeText(context, context.getResources().getString(id, values), Toast.LENGTH_SHORT).show();
    }

    public static void showToastA(Context context, int id, int index) {
        String tip = context.getResources().getStringArray(id)[index - 1];
        if (TextUtils.isEmpty(tip)) {
            return;
        }
        Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();
    }


    /**
     * view
     */
    public static void setVisibility(View context, int ctrlId, int value) {
        context.findViewById(ctrlId).setVisibility(value);
    }

    /**
     * check box
     */
    public static boolean isChecked(View context, int ctrlId) {
        return ((CheckBox) context.findViewById(ctrlId)).isChecked();
    }

    /**
     * text view
     */
    public static String getText(View context, int ctrlId) {
        return ((TextView) context.findViewById(ctrlId)).getText().toString().trim();
    }

    public static String toString(TextView context) {
        return context.getText().toString().replaceAll(" ", "");
    }

    public static float toFloat(TextView context) {
        return Float.valueOf(context.getText().toString().trim());
    }

    public static long toLong(TextView context) {
        return Long.valueOf(context.getText().toString().trim());
    }

    public static int toInteger(TextView context) {
        return Integer.valueOf(context.getText().toString().trim());
    }

    public static void setText(View context, int ctrlId, int strId) {
        ((TextView) context.findViewById(ctrlId)).setText(strId);
    }

    public static void setText(View context, int ctrlId, CharSequence content) {
        ((TextView) context.findViewById(ctrlId)).setText(content);
    }

    public static void setText(View context, int ctrlId, int strId, Object... values) {
        UI.setTvText((TextView) context.findViewById(ctrlId), strId, values);
    }


    public static void setTvText(TextView view, int strId, Object... values) {
        view.setText(view.getResources().getString(strId, values));
    }

    public static void setTvHtml(TextView view, int strId, Object... values) {
        view.setText(Spans.fromHtml(view.getResources(), strId, values));
    }


    public static <T extends View> T findViewById(View context, int ctrlId) {
        return (T) context.findViewById(ctrlId);
    }

    public static <T extends View> T findChildWithTag(ViewGroup parent, Object tag) {
        final int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            if (tag.equals(child.getTag())) {
                return (T) child;
            }
        }
        return null;
    }

    public static <T extends View> T inflate(Context context, @LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        return (T) LayoutInflater.from(context).inflate(resource, root, attachToRoot);
    }

    public static <T extends View> T inflate(Context context, @LayoutRes int resource, @Nullable ViewGroup root) {
        return (T) LayoutInflater.from(context).inflate(resource, root, false);
    }

    public static <T extends View> T inflate(LayoutInflater inflater, @LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        return (T) inflater.inflate(resource, root, attachToRoot);
    }

    public static <T extends View> T inflate(LayoutInflater inflater, @LayoutRes int resource, @Nullable ViewGroup root) {
        return (T) inflater.inflate(resource, root, false);
    }


}
 